package edu.uba.fcen.estimacion.estimacion.graphics;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import org.apache.log4j.Logger;

import edu.uba.fcen.estimacion.estimacion.Estimacion;
import edu.uba.fcen.estimacion.estimacion.EstimacionData;
import edu.uba.fcen.estimacion.estimacion.Means;

public class PerformanceVsCantPalabras {

	private static final Logger logger = Logger.getLogger(PerformanceVsCantPalabras.class);
	//Estos valores fueron calculados previamente
	private static double meanOfAgrado = 0d;
	/**
	 * @param args 1ro - el path al csv donde estan los valores de las medias
	 * 			   2do - el path al directorio de ciao 
	 * 			   3ro - la cantidad de palabras completas que hay en la base en el momento de la ejecución
	 * 
	 * Luego en R graficar con 
	 * plot(x, hits, type="o", col="blue", main=paste("Performance"), xlab=paste("Cant. de palabras"), ylab=paste("Porcentaje de aciertos"))
	 */
	public static void main(String[] args) {
		String pathToCSV = args[0];
		String pathToCiaoDirectory = args[1];
		int limitDatabase = Integer.parseInt(args[2]);
		
		File directoryBase = new File(pathToCiaoDirectory);
		List<Double> hits = new ArrayList<Double>();
		StringBuilder hitsBuilder = new StringBuilder();
		StringBuilder xBuilder = new StringBuilder();
		
		xBuilder.append("x = c(");
		hitsBuilder.append("hits = c(");
		for (int i = 50; i < limitDatabase; i+=50) {
			Estimacion estimacion = new Estimacion(pathToCSV, i);
			xBuilder.append(i).append(",");
			setMeans(estimacion, directoryBase);
			hitsBuilder.append(IterateOverFileSet(estimacion, directoryBase)).append(",");
			System.out.println("Pasada con " + i + " palabras completada!");
			estimacion.closeDB();
		}
	
		System.out.println(xBuilder.toString());
		System.out.println(hitsBuilder.toString());
	}
	private static void setMeans(Estimacion estimacion, File directoryBase) {
		DescriptiveStatistics meanStatistics = new DescriptiveStatistics();
		
		EstimacionData data;
		Means mean;
		for(File directory : directoryBase.listFiles()) {
			if (directory.isDirectory()) {
				logger.info("processing directory: " + directory.getName());
				for(File in : directory.listFiles()) {
					if (in.getName().endsWith(".freeling")) {
						data = estimacion.runOver(in.getAbsolutePath());
						mean = data.getMeans();
						Double valueMean = Double.valueOf(mean.getMeanAgrado());
						if (!valueMean.isNaN()) {
							meanStatistics.addValue(valueMean);
						}
					}
				}
			}
		}
		meanOfAgrado = meanStatistics.getMean();
		System.out.println("Mean Agrado: " + meanOfAgrado);
	}
	
	private static double IterateOverFileSet(Estimacion estimacion, File directoryBase) {
		EstimacionData data;
		Means mean;
		double yesHits = 0;
		double totalOpinions = 0;
		double totalNo = 0;
		double totalYes = 0;
		double noHits = 0;
		
		for(File directory : directoryBase.listFiles()) {
			if (directory.isDirectory()) {
				logger.info("processing directory: " + directory.getName());
				for(File in : directory.listFiles()) {
					if (in.getName().endsWith(".freeling")) {
						data = estimacion.runOver(in.getAbsolutePath());
						mean = data.getMeans();
						Double valueStdev = Double.valueOf(mean.getStdevAgrado());
						Double valueMean = Double.valueOf(mean.getMeanAgrado());
						if (valueStdev.isNaN() || valueMean.isNaN()) {
							System.out.println("Es NaN: " + in.getAbsolutePath());
						}
						
						if (in.getName().startsWith("yes")) {
							if (mean.getMeanAgrado() >= meanOfAgrado) {
								yesHits++;
							}
							totalYes++;
						}
						
						if (in.getName().startsWith("no")) {
							if (mean.getMeanAgrado() < meanOfAgrado) {
									noHits++;
							}
							totalNo++;
						} 
						totalOpinions++;
					}
				}
			}
		}
		return ((double) ((yesHits + noHits) /totalOpinions)*100);
	}

}
